<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/9/12
 * Time: 03:53
 */
namespace App\Console\Command;
require ('tool.php');
require ('output.php');
use App\Models\Program;


register_shutdown_function(function() {
    echo PHP_EOL.'shutdown'.PHP_EOL;
});

//使用ticks需要PHP 4.3.0以上版本
declare(ticks = 1);

//信号处理函数
function sig_handler($signo)
{
    switch ($signo) {
        case SIGTERM:
            // 处理kill
            echo PHP_EOL.'kill';
            exit;
            break;
        case SIGHUP:
            //处理SIGHUP信号
            break;
        case SIGINT:
            //处理ctrl+c
            echo PHP_EOL.'ctrl+c';
            exit;
            break;
        default:
            exit;
        // 处理所有其他信号
    }
}

//安装信号处理器
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP,  "sig_handler");
pcntl_signal(SIGINT,  "sig_handler");
$timer_id = swoole_timer_tick( 60*1000 , function() {
    $programs = Program::where('status', 1)->get();
    foreach ($programs as $program) {
        if (strtotime($program->apply_end_at) < time()) {
            $program->state = 2;
            $msg = "项目【{$program->title}】进入选房阶段,项目 ID 【{$program->id}】";
            \Output::stdout(\Output::COLOR_GREEN . $msg . \Output::COLOR_NONE);
            \Tool::debug($msg);
        }
        if (strtotime($program->check_end_at) < time()) {
            $program->state = 3;
            $msg = "项目【{$program->title}】已完成,项目 ID 【{$program->id}】";
            \Output::stdout(\Output::COLOR_GREEN . $msg . \Output::COLOR_NONE);
            \Tool::debug($msg);
        }
        if (strtotime($program->end_at) < time()) {
            $program->status = 2;
            $msg = "项目【{$program->title}】已过期,项目 ID 【{$program->id}】";
            \Output::stdout(\Output::COLOR_GREEN .  $msg . \Output::COLOR_NONE);
            \Tool::debug($msg);
        }
        $program->save();
    }

});
